<?php
header("Content-Type: text/html; charset=utf-8");

$mostrar_formulario = true;
$error = false;

if (isset($_REQUEST['username']) && isset($_REQUEST['email']) && isset($_REQUEST['password'])) {
    $user = $_REQUEST['username'];
    $email = $_REQUEST['email'];
    $pass = $_REQUEST['password'];
    $pass = md5($pass);
    $inserturl = "http://nibble.li/twittertido/web/insertUser.php?user=$user&email=$email&password=$pass";
    $respuesta = file_get_contents($inserturl, 0, null, null);
    if ($respuesta > 0) {
        $mostrar_formulario = false;
    } else {
        $error = true;
    }
}
?>
<!DOCTYPE html>
<html class="ui-mobile-rendering">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <title>Twittertido - Procastination everywhere!!!</title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
        <script src="js/common.js"></script>
        <script src="js/jquery.validationEngine.js"></script>
        <script src="js/jquery.validationEngine-es.js"></script>
        <link rel="stylesheet" href="css/validationEngine.jquery.css" />
        <?php if ($mostrar_formulario) { ?>

            <script>
                jQuery(document).ready(function(){
                    // binds form submission and fields to the validation engine
                    jQuery("#register").validationEngine();
                });
            </script>
        <?php } else { ?>
            <script>
                jQuery(document).onLoad(function(){
                    // binds form submission and fields to the validation engine
                    jQuery("#register").validationEngine('hideAll');
                });
            </script>
        <?php } ?>
    </head>
    <body>
        <div data-role="page" >
            <div data-role="header">
                <h1>Twittertido</h1>
            </div><!-- /header -->
            <div data-role="content" data-theme="c">

                <?php if ($mostrar_formulario) { ?>
                    <?php if ($error) { ?>
                        <div style="width: 100%; color: red; text-align: center">Ha ocurrido un error, int&eacute;ntelo de nuevo.</div>
                    <?php } ?>

                    <form id="register" name="register" method="post" action="register.php">
                        <div data-role="fieldcontain">
                            <label for="username">Nombre de usuario:</label>
                            <input type="text" name="username" id="username" class="validate[required,minSize[6],maxSize[15],ajax[ajaxUsername]]" />
                        </div>

                        <div data-role="fieldcontain">
                            <label for="email">Email:</label>
                            <input type="text" name="email" id="email" class="validate[required,custom[email],ajax[ajaxEmail]]" />
                        </div>

                        <div data-role="fieldcontain">
                            <label for="password">Password:</label>
                            <input type="password" name="password" id="password" class="validate[required,minSize[6]]" />
                        </div>
                        <div data-role="fieldcontain">
                            <label for="password_2">Confirma el password:</label>
                            <input type="password" name="password_2" id="password_2"
                                   class="validate[required,equals[password]]" value="" />
                        </div>
                        <button type="submit" data-theme="b" name="submiter">Registrar</button>
                    </form>

                <?php } else { ?>
                    <div data-theme="c" style="text-align: center; margin: 10px">
                        ¡Genial! ¡Ya eres miembro de Twittertido!<br />
                        Empieza a disfrutar de lo m&aacute;s selecto de los pajaritos azules
                    </div>
                    <a href="index.php" data-role="button">Aceptar</a> 
                <?php } ?>
            </div>

        </div>
    </body>
</html>